﻿//分辨率设置
getCostsystemFbl(systemFblW,systemFblH)
//---------------------------------------------------个人申请表格处理--start----------------------------
$("#easyui-panel_div").panel({
    onExpand:function () {
        $("#selftask_table").datagrid({
            pagination:true,
            striped:true,
            fitColumns:true,
            rownumbers:true,
            striped:true,
            nowrap:false,
            url:'/examine/queryelfsubmittask',
            idField:'processInstanceId',
            singleSelect:true,
            columns:[[
                {field:'processInstanceId',title:'实例id',hidden:true},
                {field:'processDefinitionName',title:'请求',width:40,align:'center'},
                {field:'businessKey',title:'报表单据',width:40,align:'center',formatter: function(value,row,index){
                        return '<a href="#" costpostId="'+value+'" onclick="costReportSelect2(this)">'+value+'</a>';
                    }
                },
                {field:'id',title:'物料品号',width:40,align:'center'},
                {field:'name',title:'物料名称',width:200,align:'center'},
                {field:'localizedDescription',title:'实际报价',width:50,align:'center'},
                {field:'deploymentId',title:'整体毛利',width:50,align:'center'},
                {field:'tenantId',title:'整体毛利率',width:50,align:'center'},
                {field:'startTime',title:'发起时间',width:40,align:'center'},
                {field:'endTime',title:'最终审批结果',width:30,align:'center',formatter: function(value,row,index){
                        if (value==null){
                            return "审批中";
                        } else {
                            return "审批结束";
                        }
                    }
                },
                {field:'selftask_t',title:'查看进展',width:30,align:'center',formatter: function(value,row,index){
                        var businessKey = row.businessKey;
                        //传过去会报错 onclick="costItem('+value+')"貌似只能传数字和this
                        var htm='<a href="#" businessKey="'+businessKey+'" processInstanceId="'+row.processInstanceId+'"  onclick="costItem(this)" class="easyui-linkbutton" data-options="iconCls:\'icon-search\'">详情</a>';
                        return htm;
                    }
                }
            ]]

        })
    }

})
function costItem(a){
    var businessKey=a.getAttribute("businessKey")
    var processInstanceId=a.getAttribute("processInstanceId")
    //获取选中的行
    $('#selftask_dialog').dialog({
        title: '【'+businessKey+'】成本报告处理进度',
        width: costsystemFblW,
        height: costsystemFblH,
        cache:true,
        modal: true,
        resizable:true,
        href:'/costReportForm/taskItem.html',
        onLoad:function () {
            $.ajax({
                url:'/examine/getAllProcessInstanceTask/'+processInstanceId,
                success:function (data) {
                    $('#taskItem_table').datagrid("loadData", data);
                }
            });
        },
        onClose:function () {
            //页面taskItem.html的div
            $('#taskItem').remove();
        }

    })
}
//---------------------------------------------------个人申请表格处理--end----------------------------
// -----------------------------任务 start-----------------
//初始化表格
var reporExamineTable=$('#reporExamine_table').datagrid({
    striped:true,
    rownumbers:true,
    fitColumns:true,
    nowrap:false,
    url:'/examine/queryselftask/costReportProcess',
    idField:'approvalsProcinst',
    singleSelect:true,
    onLoadError:function(){
        alert("数据发生异常了！");
    },
    columns:[[
        {field:'approvalsProposer',title:'申请人',align:'center',width:50,align:'center'},
        {field:'approvalsDescription',title:'申请要求',align:'center',width:50,align:'center'},
        {field:'approvalsReceipts',title:'申请单据',align:'center',width:50,formatter: function(value,row,index){
                return '<a href="#" approvalsReceipts="'+value+'" onclick="skipDanju(this)">'+value+'</a>';
            }
        },
        {field:'finishedProductCode',title:'物料品号',width:50,align:'center'},
        {field:'productName',title:'物料名称',width:200,align:'center'},
        {field:'realityQuotedPrice',title:'实际报价',width:50,align:'center'},
        {field:'grossMarginMoney',title:'整体毛利',width:50,align:'center'},
        {field:'grossMarginRate',title:'整体毛利率',width:50,align:'center'},
        {field:'approvalsProcinst',title:'实例ID',hidden:true},
        {field:'approvalsAuditopinion',title:'审批结果描述',width:150,align:'center'},
        {field:'bbb',title:'操作',align:'center',width:80,formatter: function(value,row,index) {
            //获取状态：拒绝的状态 3  则是申请人要进行修改，状态1是同意，则是审批操作，row.ccc是财务
                //  获取状态
              var data=  commonAjax("get","/costReport/getApproverOperationStatus",{"processInstanceId":row.approvalsProcinst})
                //console.log(data)
                //console.log(row)
                var userName = $("#indexSessionUserName").text();
                var htm;
                //根据拒绝3状态给予修改按钮
                if (data==3){
                    htm = $('#reporExamine_button2').html();
                }else if (row.ccc) {  //财务
                    htm = $('#reporExamine_button3').html();
                }else{      //审批人节点
                        htm=$('#reporExamine_button').html();
                }

                return htm;
            }
        }
    ]]
});

function approveSubmit(content,num,result) {
    //获取选中的行
    var r = reporExamineTable.datagrid('getSelected');
    var approvalsAuditopinion=$("#approvalsAuditopinion").textbox("getValue");
    r.approvalsAuditopinion=approvalsAuditopinion;
    r.num=num;
    r.result=result;
    delete r.approvalsCreated;  //去除时间属性
    r.approvalsId=null;         //把主键id去除
    delete r.ccc;
    if(r){
        $.messager.confirm('确认提示框','您确认'+content+'<span style="color:red;">'+r.approvalsReceipts+'</span>单据吗？',function(c){
            if (c){

                if (content=="提交"){//被拒绝后重新修改保存后提交审核
                    $('#costReport_Form').form('submit', {
                        queryParams: costReportIndexSaveData(),
                        url: "/costReport/costReportSave",
                        onSubmit: function () {
                            var isValid = $(this).form('validate');
                            if (isValid){
                                var v=  $('#v_a_t_export_tax_rate').numberbox('getValue');
                                if (v==0){
                                    layer.msg("T2税项未填写！")
                                    return false;
                                }
                                var v1=  $('#accessories_loss_rate').numberbox('getValue');
                                if (v1==0.00 || v1 ==""){
                                    layer.msg("配件耗损率未填写！")
                                    return false;
                                }

                            }
                            return isValid;	// 返回false终止表单提交
                        },
                        success: function (data) {
                            var data = eval('(' + data + ')');
                            if (data.statu == 200) {
                                //重新修改后提交的标识：后端判断把状态改为5：证明是重新提交的状态
                                r.resubmit='666';
                                $.post({
                                    url:'/examine/approvalprocess',
                                    data:r,
                                    success:function(data){
                                        if(data.statu==200){
                                            $("#reporExamine__dialog").dialog("close")
                                            reporExamineTable.datagrid('reload');
                                        }
                                        data={statu:200,message:"提交审核成功"}
                                        windowsOperation('reporExamine__dialog', null, data)
                                        $('#costReport_dlg_selfTask').dialog("close");
                                    }
                                })

                            } else {
                                layer.msg(data.message)
                            }
                        },
                        onLoadError: function (data) {
                            layer.msg("服务器维护中")
                        }
                    });
                }else {
                    r.resubmit='222';
                    $.post({
                        url:'/examine/approvalprocess',
                        data:r,
                        success:function(data){
                            if(data.statu==200){
                                $("#reporExamine__dialog").dialog("close")
                                $("#costReport_dlg_selfTask").dialog("close")
                                reporExamineTable.datagrid('reload');
                                //刷新历史详情表格
                                $("#selftask_examined_table").datagrid('reload')
                            }
                            layer.msg(data.message);
                        }
                    })
                }

            }
        });
    }else{
        topCommonMessage('哎呀,您点的太快了，麻烦再点一下吧！');
    }
}

//任务表中的详情列表
function costItem2(){
    //获取选中的行
    var r = reporExamineTable.datagrid('getSelections');
    var processInstanceId = r[0].approvalsProcinst;
    var businessKey = r[0].approvalsReceipts;
    $('#reporExamine_dialog').dialog({
        title: '【'+businessKey+'】成本报告处理进度',
        width: costsystemFblW,
        height: costsystemFblH,
        cache:true,
        modal: true,
        href:'/costReportForm/taskItem.html',
        onLoad:function () {
            if(r.length>0){
                $.ajax({
                    url:'/examine/getAllProcessInstanceTask/'+processInstanceId,
                    success:function (data) {
                        $('#taskItem_table').datagrid("loadData", data);
                    }
                });

            }else{
                topCommonMessage('哎呀,您点的太快了，麻烦再点一下吧！');
            }
        },
        onClose:function () {
            $('#taskItem').remove();
        }
    })
}

//申请人的【修改】按钮操作
function modifyCostR() {

    var r = reporExamineTable.datagrid('getSelected');
    //由于成本编码会触发事件刷出所有的配件，设置了全局变量，让其第一次刷新不出数据
    costReportUpdateAndCoopyC=false;
    var msg = commonAjax('GET', '/costReport/isCostReportStatus', {"banben": r.approvalsReceipts});
    if (msg) {
        //1、审核完(全部同意)，2、审核中的两个状态不能进行修改
        var costReportStatus = msg.costReportStatus;
        if (costReportStatus!=1 && costReportStatus != 3){
            $("#costReport_dlg_selfTask").dialog({
                width:costsystemFblW,
                height:costsystemFblH,
                closed:false,
                cache:false,
                href:'/costReportForm/costReportForm.html',
                buttons:[{
                    text:'重新提交审核',
                    handler:function(){reporExamine_fuction_dialog(0)}
                }],
                onLoad:function(){ //内容加载完后触发
                    $('#finishedProductCode').textbox('readonly', false);//成本编码更改为可修改
                    var data = commonAjax('GET', '/costReportForm/getCostReportABC', {"banben": r.approvalsReceipts});
                    costReport = data.table[0].costReport;

                    var path=costReport.costImagePath;
                    if (path){
                        $("#costReportFormImageDiv").show()
                        $("#costReportFormImage").attr("src",path+"?ran=" + Math.random())
                        $("#costImagePathDiv").hide();
                    }
                    $('#costReport_Form').form('load', costReport);
                    //因为默认是给隐藏掉的成品编码赋值的，这里需要为id为：finishedProductCodeZt的进行赋值，
                    var finishedProductCodeZt= $("#finishedProductCodeZt");
                    if(finishedProductCodeZt){
                        $("#finishedProductCodeZt").textbox("setValue",costReport.finishedProductCode)
                    }
                    $('#costReport_Form').form('load', costReport);
                    $("#cost_report_b").datagrid("loadData", data.table[0].b);
                    $("#cost_report_c").datagrid("loadData", data.table[0].c);
                    $("#cost_report_a").datagrid("loadData", data.table[0].a);
                    cost_report_b_count();//B人工总价格
                    cost_report_c_count();//C配件费总价格
                    cost_report_a_reloadFooter(); //A 水料价格
                    oneUpdateShow(costReport.vatexportTaxRate);
                    $('#reality_quoted_price').numberbox('enable') //启用实际报价输入
                },
                onClose:function () {
                    $("#costReport_Form_div").remove();
                }
            })
            $('#costReport_dlg_selfTask').dialog('center');
        }else {
            layer.msg("审核中或已封存的不能进行修改！")
        }
    } else {
        layer.msg("没有相关数据!")
    }
}

//任务中---操作--明细触发方法
function selfTaskApprovalCostReport() {
    var banben = reporExamineTable.datagrid('getSelected').approvalsReceipts;
    $("#costReport_dlg_selfTask").dialog({
        title:"【"+banben+"】成本报告表",
        width:costsystemFblW,
        height:costsystemFblH,
        closed:false,
        cache:false,
        href:'/costReportForm/costReportFormStatic.html',
        onLoad:function(){ //内容加载完后触发
           // console.log(12312)
            var data = commonAjax('GET', '/costReportForm/getCostReportABC', {"banben": banben});
            costReport = data.table[0].costReport;
            var path=costReport.costImagePath;
            if (path){
                $("#costReportFormImageStatic").show()
                $("#costReportFormImageStatic").attr("src",path+"?ran=" + Math.random())
                $("#costImagePathDivStatic").hide();
            }
            $('#reportFormStatic_Form').form('load', costReport);
            $("#cost_report_static_a").datagrid("loadData", data.table[0].a);
            $("#cost_report_static_b").datagrid("loadData", data.table[0].b);
            $("#cost_report_static_c").datagrid("loadData", data.table[0].c);
            costReportBSinglehoursItem=data.table[0].bs;
        },
        buttons:[{
            iconCls:"icon-search",
            text:'审批详情',
            handler:function(){costItem2()}
        },{
            iconCls:"icon-ok",
            text:'审批',
            handler:function(){reporExamine_fuction_dialog(1)}
        },{
            iconCls:"icon-cancel",
            text:'退出',
            handler:function(){$('#costReport_dlg_selfTask').dialog('close')}
        }],
        onClose:function () {
             $('#costReportFormStatic').remove();
        }
    })
    $('#costReport_dlg_selfTask').dialog('center');
}

function costReportQuery(banben) {
    $("#costReport_dlg_selfTask").dialog({
        title:"成本报告表",
        width:costsystemFblW,
        height:costsystemFblH,
        closed:false,
        href:'/costReportForm/costReportFormStatic.html',
        buttons:[],
        onLoad:function(){ //内容加载完后触发
            var data = commonAjax('GET', '/costReportForm/getCostReportABC', {"banben": banben});
            costReport = data.table[0].costReport;
            var path=costReport.costImagePath;
            if (path){
                $("#costReportFormImageStatic").show()
                $("#costReportFormImageStatic").attr("src",path+"?ran=" + Math.random())
                $("#costImagePathDivStatic").hide();
            }
            $('#reportFormStatic_Form').form('load', costReport);
            $("#cost_report_static_a").datagrid("loadData", data.table[0].a);
            $("#cost_report_static_b").datagrid("loadData", data.table[0].b);
            $("#cost_report_static_c").datagrid("loadData", data.table[0].c);
            costReportBSinglehoursItem=data.table[0].bs;
        },
        onClose:function () {
            $("#costReport_Form_div2").remove();
        }
    })
    $('#costReport_dlg_selfTask').dialog('center');
}

function reporExamine_fuction_dialog(bool) {
    var reporExamineDialogButton;
    if (bool){
        reporExamineDialogButton=[{
            text:'同意',
            handler:function(){approveSubmit('同意',1,1)}
        },{
            text:'拒绝',
            handler:function(){approveSubmit('拒绝',0,0)}
        }]
    }else {
        reporExamineDialogButton=[{
            text:'提交',
            handler:function(){approveSubmit('提交',1,1)}
        }]
    }
    $("#reporExamine__dialog").dialog({
        title:'内容',
        closed:false,
        buttons:reporExamineDialogButton
    })
}

//审批成本明细reporExamine.html，selfTask.html：跳转单据详情页面
function skipDanju(a) {
    var val=a.getAttribute("approvalsReceipts");
    reporExamineTable.datagrid({
        onSelect:function (index, row) {
            val=row.approvalsReceipts
        }
    })
    costReportQuery(val);
}


function costReportSelect2(a) {
    var banben=a.getAttribute("costPostId");
    if (banben != null && banben != '') {
        $.ajax({
            type: "GET",
            url: '/costReportForm/getCostReportGrf?banben=' + banben,
            success: function (msg) {
                if (msg.status == 200) {
                    window.open("/costReportForm/queryCostReportForm.html?banben=" + banben)
                }
                if (msg.status == 500) {
                    topCommonMessage("没有相关数据!")
                }
            }
        });
    }else {
        topCommonMessage("请填写版本号查询!")
    }
}

//-----------------------------------------------------------------历史详情----start---------------------

//报表单据格式化方法
function selftaskExaminedTableBusinessKey(value,row,index) {
    return '<a href="#" approvalsReceipts="'+value+'" onclick="skipDanju(this)">'+value+'</a>';
}
//最终审批结果格式化方法
function selftaskExaminedTableEndTime(value,row,index) {
    if (value==null){
        return "审批中";
    } else {
        return "审批结束";
    }
}
//查看进展格式化方法S
function selftaskExaminedTableelftaskT(value,row,index) {
    var businessKey = row.costPostId;
    //传过去会报错 onclick="costItem('+value+')"貌似只能传数字和this
    var htm='<a href="#" businessKey="'+businessKey+'" processInstanceId="'+row.processInstanceId+'"  onclick="costItem(this)" class="easyui-linkbutton" data-options="iconCls:\'icon-search\'">详情</a>';
    return htm;
}
